<?php
include ("../main/inc/global.inc.php");

require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
require_once (api_get_path(CONFIGURATION_PATH).'profile.conf.php');

api_block_anonymous_users(); // only users who are logged in can proceed

// Top part of the HTML
Display :: display_header('File List');
?>

<?php
$link = mysql_connect("localhost", "root", "root");
mysql_select_db("dokeos_main", $link);


if (isset($_POST['sharefile_fileId']) && isset($_POST['sharefile_username']) )
{
	$userName = $_POST['sharefile_username'];	
	$sql_finduserId = "SELECT * from user
	WHERE username = '".$userName."'";
	$result_userId = mysql_query($sql_finduserId, $link);
	$row = mysql_fetch_array($result_userId, MYSQL_ASSOC);
	$userId= $row['user_id'];
	$fileId = $_POST['sharefile_fileId'];
	$sql_verify = "SELECT * from file_user WHERE fileId=$fileId AND userid = ".$userId;
	$result_verify = mysql_query($sql_verify, $link);
	
	if(mysql_num_rows($result_verify)!=0) {
		echo "File already shared to user " . $userName;
	} else {
		$sql_invite = "INSERT INTO file_user_invitation VALUES ($fileId, $userId, 0, NOW(), '".$_POST['sharefile_userAccess']."')";
  	$result = mysql_query($sql_invite, $link);
		if($result){
			echo "Invitation is sent to user $userName";
		} else {
			echo "Invitation has already been sent to user $userName";
		}
	}
}	

mysql_close($link);


?>

<?php
// Footer
Display :: display_footer();
?>
